var express = require('express')
var router = express.Router()
const { getAll, getById, exec } = require('../db/index')

//POST {username:'',email:'',password:''}
router.post('/',async (req,res) => {
  const {username,email,password} = req.body
  let sql = `insert into admins (username,password,email) values ('${username}','${password}','${email}') `
  const data = await exec(sql)
  res.send({
    code:0,
    message:'注册用户成功'
  })
})

//检查用户名是否已被占用
router.get('/checkName',async(req,res) => {
  const {username} = req.query
  let sql = `select * from admins where username=${username}`
  const data = await exec(sql)
  res.send({
    code:0,
    message:'查询用户名成功',
    result:data
  })
})

//检查邮箱是否已被占用
router.get('/checkEmail',async(req,res) => {
  const {email} = req.query
  let sql = `select * from admins where email=${email}`
  const data = await exec(sql)
  console.log(data)
  res.send({
    code:0,
    message:'查询邮箱成功',
    result:data
  })
})
module.exports = router
